function gadget:GetInfo()
	return {
		name      = "Win Condition Selection & Guess handling",
		desc      = "Controls win condition selection and player guesses.",
		author    = "B.Tyler, M. Lerner, FLOZi (C.Lawrence)",
		date      = "Nov 6th, 2010",
		license   = "GNU GPL v2",
		layer     = 1,
		enabled   = true  --  loaded by default?
  }
end


local spawnDefs = include("luarules/configs/spawn_defs.lua")

-- function localisations
-- Synced Read
local GetUnitDefID			= Spring.GetUnitDefID
local GetUnitPosition		= Spring.GetUnitPosition
local GetUnitsInCylinder	= Spring.GetUnitsInCylinder
local ValidUnitID			= Spring.ValidUnitID
local GetTeamList			= Spring.GetTeamList
-- Synced Ctrl
local DestroyUnit			= Spring.DestroyUnit



-- Constants
local teams = {}
GG['donePicking'] = false
--I'm so sorry...
local commands = {[35522] = 35522, [35523] = 35523, [35524] = 35524, [35525] = 35525}

local winDescriptions = {
	[1] = {
		name	= "KOTH",
		action	= "clearmines",
		id		= commands[35522],
		type	= CMDTYPE.ICON, 
		tooltip	= "Win with KING OF THE HILL",
	},
	[2] = {
		name	= "KILL",
		action	= "clearmines",
		id		= commands[35523],
		type	= CMDTYPE.ICON, 
		tooltip	= "Win with KILLS",
	},
	[3] = {
		name	= "CTFS",
		action	= "clearmines",
		id		= commands[35524],
		type	= CMDTYPE.ICON, 
		tooltip	= "Win with CTF",
	},
	[4] = {
		name	= "LIES",
		action	= "clearmines",
		id		= commands[35525],
		type	= CMDTYPE.ICON, 
		tooltip	= "Win with LIES.",
	},
}

-- Variables

if gadgetHandler:IsSyncedCode() then
--	SYNCED

--	CallIns

function gadget:AllowCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
	if commands[cmdID] then
		local ud = UnitDefs[unitDefID]
		local cp = ud.customParams
		if cp and cp.guess_handler == "1" then
			return true
		else
			return false
		end
	else
		return true
	end
end


function gadget:CommandFallback(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
	if commands[cmdID] and teams[teamID + 1] == false then
		local x, y, z = GetUnitPosition(unitID)
		if cmdID == commands[35522] then
			for i, unitName in ipairs(spawnDefs["koth"]) do
				Spring.CreateUnit(unitName, x+math.random(-i*50, i*50), y, z+math.random(-i*50, i*50), 0, teamID)
				GG['koth'][teamID+1] = teamID
			end
		elseif cmdID == commands[35523] then
			for i, unitName in ipairs(spawnDefs["kills"]) do
				Spring.CreateUnit(unitName, x+math.random(-i*50, i*50), y, z+math.random(-i*50, i*50), 0, teamID)
				GG['kills'][teamID+1] = teamID
			end
		elseif cmdID == commands[35524] then
			for i, unitName in ipairs(spawnDefs["conversions"]) do
				Spring.CreateUnit(unitName, x+math.random(-i*50, i*50), y, z+math.random(-i*50, i*50), 0, teamID)
				GG['conversions'][teamID+1] = teamID
			end
		else 
			for i, unitName in ipairs(spawnDefs["deception"]) do
				Spring.CreateUnit(unitName, x+math.random(-i*50, i*50), y, z+math.random(-i*50, i*50), 0, teamID)
				GG['deception'][teamID+1] = teamID
			end
		end
		teams[teamID+1] = cmdID
		
		local teamsDone = 0
		for i, pickStatus in ipairs(teams) do
			if teams[i] ~= false then
				teamsDone = teamsDone + 1
			end
		end
		if teamsDone == #teams-1 then
			Spring.Echo("done picking!")
			GG['donePicking'] = true
			
		end
		
	elseif commands[cmdID] and GG['donePicking'] == true then
		if cmdID == commands[35522] then
			Spring.Echo("Guessing KOTH!")
		elseif cmdID == commands[35523] then
			Spring.Echo("Guessing KILLER!")
		elseif cmdID == commands[35524] then
			Spring.Echo("Guessing CTF!")
		else 
			Spring.Echo("Guessing LIES!")
		end
		for i=1, #teams-1 do
			local winCondition = teams[i]
			if cmdID == winCondition and (i - 1) ~= teamID then
				Spring.Echo("Successful guess!, team" .. teamID .." has won!")
				GG.gameOver(teamID, i-1)
			elseif cmdID ~= winCondition and (i - 1) ~= teamID then
				Spring.Echo("Unsuccessful guess, team" .. teamID .." has lost.")
				GG.gameOver(i-1, teamID)
			else
			end
		end
	else
		return false
	end
end


function gadget:UnitCreated(unitID, unitDefID, teamID)
	local ud = UnitDefs[unitDefID]
	local cp = ud.customParams
	if cp and cp.guess_handler == "1" then
		for i=1, #winDescriptions do
			Spring.InsertUnitCmdDesc(unitID, winDescriptions[i])
		end
	end
end


function gadget:Initialize()
	teams = GetTeamList()
	GG['koth'] = {-1, -1}
	GG['kills'] = {-1, -1}
	GG['conversions'] = {-1, -1}
	GG['deception'] = {-1, -1}
	GG['gameEnded'] = false
	for i, pickStatus in ipairs(teams) do
		Spring.Echo("team number:" .. i, teams[i])
		teams[i] = false
	end
	gadgetHandler:RegisterCMDID(commands[35522])
	gadgetHandler:RegisterCMDID(commands[35523])
	gadgetHandler:RegisterCMDID(commands[35524])
	gadgetHandler:RegisterCMDID(commands[35525])
	-- Fake UnitCreated events for existing units. (for '/luarules reload')
	Spring.Echo(commands)
	local allUnits = Spring.GetAllUnits()
	for i=1,#allUnits do
		local unitID = allUnits[i]
		gadget:UnitCreated(unitID, Spring.GetUnitDefID(unitID))
	end
end

else
--	UNSYNCED

end